//
//  WBPopMenuSingleton.m
//  writingDemo
//
//  Created by 周佳兴 on 16/3/17.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "WBPopMenuSingleton.h"
#import "WBPopMenuView.h"
#import "Masonry.h"
#import "UIColor+CL.h"
@interface WBPopMenuSingleton ()
@property (nonatomic, strong) WBPopMenuView * popMenuView;
@end

@implementation WBPopMenuSingleton

+ (WBPopMenuSingleton *) shareManager {
    static WBPopMenuSingleton *_PopMenuSingleton;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _PopMenuSingleton = [[WBPopMenuSingleton alloc]init];
    });
    return _PopMenuSingleton;
}

- (void) showPopMenuSelecteWithFrameWidth:(CGFloat)width andFrame:(CGRect)frame
                                     item:(NSArray *)item
                                   action:(void (^)(NSInteger))action {
    self.action = action;
    
    __weak __typeof(&*self)weakSelf = self;
    if (self.popMenuView != nil) {
        [weakSelf hideMenu];
    }
    UIWindow * window = [[[UIApplication sharedApplication] windows] firstObject];
    self.popMenuView = [[WBPopMenuView alloc]initWithFrame:window.bounds
                                                 menuWidth:width
                                                     items:item
                                                    action:^(NSInteger index) {
                                                        action(index);
                                                        [weakSelf hideMenu];
                                                    }];
    self.popMenuView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [window addSubview:self.popMenuView];
    _iv = [[UIView alloc] init];
    _testView = [[UIView alloc] init];
    
    //    NSLog(@"%@",NSStringFromCGRect(self.popMenuView.tableView.frame));
    //    CGRect transformFrame = self.popMenuView.tableView.frame;
    CGRect transformFrame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-width/2, 69, width, 0);
    self.popMenuView.tableView.frame = transformFrame;
    
    CGRect testViewFrame = transformFrame;
    testViewFrame.origin.y += 10;
//    testViewFrame.size.height -=10;
    _iv.frame = testViewFrame;
    _testView.frame = transformFrame;
    
    NSLog(@"%@",NSStringFromCGRect(self.popMenuView.tableView.frame));
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect transformEndFrame = transformFrame;
        transformEndFrame.size.height = 40*item.count;
        self.popMenuView.tableView.frame = transformEndFrame;
        transformEndFrame.origin.y = transformEndFrame.origin.y-transformEndFrame.size.height/2;//由于中心点坐标变动导致向下浮动半个height的距离需要改回去
        CGRect testViewEndFrame = transformEndFrame;
        testViewEndFrame.origin.y += 10;
        testViewEndFrame.size.height -=10;
        
        _testView.frame = transformEndFrame;
//        [UIView animateWithDuration:0.3 animations:^{
            _iv.frame = testViewEndFrame;
//        }];
        
    }];
    
    
    
    //    testView.backgroundColor = [UIColor redColor];
    _testView.backgroundColor=[UIColor whiteColor];
    //v.layer.masksToBounds=YES;这行去掉
    _testView.layer.cornerRadius=10;
    _testView.layer.anchorPoint = CGPointMake(0.5, 0);//设置缩放中心点坐标
//    _testView.layer.shadowColor=[UIColor colorWithHex:0x666666].CGColor;
//    _testView.layer.shadowOffset=CGSizeMake(10, 10);
//    _testView.layer.shadowOpacity=0.5;
//    _testView.layer.shadowRadius=5;
//    _testView.hidden = YES;
    
    [self.popMenuView addSubview:_iv];
    _iv.backgroundColor = [UIColor whiteColor];
//    _testView.backgroundColor=[UIColor whiteColor];
    //v.layer.masksToBounds=YES;这行去掉
    _iv.layer.cornerRadius=10;
    _iv.layer.anchorPoint = CGPointMake(0.5, 0);//设置缩放中心点坐标
    _iv.layer.shadowColor=[UIColor blackColor].CGColor;
    _iv.layer.shadowOffset=CGSizeMake(3, 3);
    _iv.layer.shadowOpacity=0.5;
    _iv.layer.shouldRasterize = YES;
    _iv.layer.shadowRadius=5;
    
    
    
//    testView.hidden = YES;
    [self.popMenuView addSubview:_testView];
//    [_testView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(self.popMenuView.tableView.mas_height);
//        make.left.equalTo(self.popMenuView.tableView.mas_left);
//        make.right.equalTo(self.popMenuView.tableView.mas_right);
//        make.top.equalTo(self.popMenuView.tableView.mas_top).offset(-self.popMenuView.tableView.frame.size.height/2);
//    }];
    [self.popMenuView sendSubviewToBack:_testView];
    [self.popMenuView sendSubviewToBack:_iv];
    
//        [UIView animateWithDuration:0.3 animations:^{
//            self.popMenuView.tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
//        }];
    
    
}

- (void) hideMenu {
    
    self.action(999);
    
    [UIView animateWithDuration:0.15 animations:^{
        self.popMenuView.tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
//        _testView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
        
//        CGPoint point = _testView.layer.anchorPoint;
//        NSLog(@"%@",NSStringFromCGPoint(point));
        
        _testView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
        _iv.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
    } completion:^(BOOL finished) {
        
        
        
        [self.popMenuView.tableView removeFromSuperview];
        [self.popMenuView removeFromSuperview];
        self.popMenuView.tableView = nil;
        self.popMenuView = nil;
    }];
}



@end
